package tech.jabari.user.controller;

import tech.jabari.utils.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询用户名是否可用。<br/>
 * 1:代表可以使用；0：代表不可以使用。
 */
@WebServlet("/checkUsername")
public class CheckUsernameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取用户名： username
        String username = request.getParameter("username");
        System.out.println("------username = " + username);
        try {
            Thread.sleep(1000);
            System.out.println("------10s-----");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //2.用户名校验: select * from t_user where username = ?
        String res = "1";
        List queryList = JdbcUtil.query("select * from t_user where username = ?", username);
        // 1代表：可以使用；0代表：不可以使用
        if (null != queryList && queryList.size() > 0) {
            res = "0";
        }
        System.out.println("------res = " + res);
        response.getWriter().write(res);
    }
}
